package main

func groupAnagrams(strs []string) [][]string {
	mp := map[string][]string{}
	idxKeys := make([]string, 0)
	for _, s := range strs {
		key := SortString(s)
		_, ok := mp[key]
		if !ok {
			idxKeys = append(idxKeys, key)
		}
		mp[key] = append(mp[key], s)
	}
	data := make([][]string, 0, len(mp))
	for i := len(idxKeys) - 1; i >= 0; i-- {
		key := idxKeys[i]
		data = append(data, mp[key])
	}
	return data
}

func SortString(src string) (key string) {
	sBytes := []byte(src)
	sort.Slice(sBytes, func(i, j int) bool { return sBytes[i] < sBytes[j] })
	key = string(sBytes)
	return key
}
